package cn.com.oceansoft.business.danger.service;

import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.danger.entity.Attach;
import cn.com.oceansoft.business.danger.entity.Ptcsbz;
import cn.com.oceansoft.business.danger.entity.PtcsbzFlow;
import cn.com.oceansoft.business.danger.persistence.IPtcsbzMapper;
import cn.com.oceansoft.business.danger.util.ExeclUtil;
import cn.com.oceansoft.nos.common.util.web.PageUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by OS on 2018/5/28.
 */

@Service
@Transactional
public class PtcsbzServiceImpl implements IPtcsbzService {
    @Resource
    private IPtcsbzMapper ptcsbzMapper;

    @Override
    public Pager<Ptcsbz> getPtcsbzList(Query query) {
        try {
            PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
            Page<Ptcsbz> page = (Page<Ptcsbz>) ptcsbzMapper.getPtcsbzList(query);
            return PageUtil.page2pager(page, query);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void PtcsbzQrth(PtcsbzFlow ptcsbzFlow, String isconfirm) {
        try {
            ptcsbzMapper.addPtcsbzFlow(ptcsbzFlow);
            ptcsbzMapper.editPtcsbz(ptcsbzFlow.getPtcsbzid(), isconfirm);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Ptcsbz findByid(String id) {
        return ptcsbzMapper.findByid(id);
    }

    @Override
    public List<Attach> findAllAttach(String id) {
        return ptcsbzMapper.findAllAttach(id);
    }

    @Override
    public List<PtcsbzFlow> findAllFlowsbyPtcsbzd(String id) {
        return ptcsbzMapper.findAllFlowsbyPtcsbzd(id);
    }

    @Override
    public List<Ptcsbz> ptcsbztj(int deep, Query query) {
        if (deep == 1) {
            return ptcsbzMapper.ptcsbztj1(query);
        } else if (deep == 2) {
            return ptcsbzMapper.ptcsbztj2(query);
        } else if (deep == 3) {
            return ptcsbzMapper.ptcsbztj3(query);
        } else {
            return ptcsbzMapper.ptcsbztj4(query);
        }
    }

    @Override
    public void exportPtcsbztj(HttpServletResponse response, List<Ptcsbz> list, int deep) {
        String sheet1 = "配套措施保障统计表";
        String[] sheets = new String[]{sheet1};
        Map<String, String[]> titles = new HashMap<>();
        String ss = "区";
        if (deep == 1) {
            ss = "区";
        } else if (deep == 2) {
            ss = "街道";
        } else {
            ss = "社区";
        }
        String[] title = new String[]{"序号", ss, "灭火器", "应急照明灯", "简易喷淋", "独立烟感报警器", "电气火灾监控系统", "可燃气体紧急切断装置", "电动车设施个数", "电动车设施面积", "电动车停放数量"};
        titles.put(sheet1, title);
        Map<String, List<String[]>> values = new HashMap<>();
        List<String[]> value = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Ptcsbz ptcsbz = list.get(i);
            String[] str = new String[]{i + 1 + "", ptcsbz.getName(), ptcsbz.getMhq() + "",
                    ptcsbz.getYjzmd() + "", ptcsbz.getJypl() + "", ptcsbz.getDlygbjq() + "", ptcsbz.getDqhzjkxt() + "",
                    ptcsbz.getKrqtjjqdzz() + "",ptcsbz.getDdctfl() + "", ptcsbz.getDdcmj() + "",  ptcsbz.getDdcgs() + ""};
            value.add(str);
        }
        values.put(sheet1, value);
        ExeclUtil.createExecl("配套措施保障统计表", sheets, titles, values, response);
    }

    @Override
    public Pager<Ptcsbz> getPtcsbzDetailList(Query query) {
        try {
            PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
            Page<Ptcsbz> page = (Page<Ptcsbz>) ptcsbzMapper.getPtcsbzDetailList(query);
            return PageUtil.page2pager(page, query);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
